package com.chinatechstar.admin.controller;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.chinatechstar.admin.entity.SysDict;
import com.chinatechstar.admin.service.SysDictService;
import com.chinatechstar.admin.vo.SysDictVO;
import com.chinatechstar.component.commons.result.ActionResult;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.utils.ExcelUtils;
import com.chinatechstar.component.commons.validator.InsertValidator;
import com.chinatechstar.component.commons.validator.UpdateValidator;

/**
 * 字典信息的控制层
 */
@Api(value = "数据字典的控制层", tags = {"数据字典"})
@RestController
@RequestMapping("/sysdict")
public class SysDictController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private SysDictService sysDictService;

    /**
     * 查询字典分页
     *
     * @param sysDictVO 字典前端参数
     * @return
     */
    @ApiOperation(value = "查询字典分页", tags = {"数据字典"})
    @GetMapping(path = "/querySysDict")
    public ListResult<Object> querySysDict(SysDictVO sysDictVO) {
        Map<String, Object> data = sysDictService.querySysDict(sysDictVO.getCurrentPage(), sysDictVO.getPageSize(), sysDictVO.getDictType(),
                sysDictVO.getDictName(), sysDictVO.getDictValue());
        return ResultBuilder.buildListSuccess(data);
    }

    /**
     * 查询字典的树数据
     *
     * @return
     */
    @ApiOperation(value = "查询字典的树数据", tags = {"数据字典"})
    @GetMapping(path = "/querySysDictTree")
    public ListResult<Object> querySysDictTree() {
        LinkedHashMap<String, Object> data = sysDictService.querySysDictTree();
        return ResultBuilder.buildListSuccess(data);
    }

    /**
     * 新增字典
     *
     * @param sysDict 字典对象
     * @return
     */
    @ApiOperation(value = "新增字典", tags = {"数据字典"})
    @PostMapping(path = "/addSysDict")
    public ActionResult addSysDict(@Validated(InsertValidator.class) @RequestBody SysDict sysDict) {
        sysDictService.insertSysDict(sysDict);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 编辑字典
     *
     * @param sysDict 字典对象
     * @return
     */
    @ApiOperation(value = "编辑字典", tags = {"数据字典"})
    @PutMapping(path = "/updateSysDict")
    public ActionResult updateSysDict(@Validated(UpdateValidator.class) @RequestBody SysDict sysDict) {
        sysDictService.updateSysDict(sysDict);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 删除字典
     *
     * @param id 字典ID
     * @return
     */
    @ApiOperation(value = "删除字典", tags = {"数据字典"})
    @PostMapping(path = "/deleteSysDict")
    public ActionResult deleteSysDict(@RequestParam(name = "id") Long[] id) {
        sysDictService.deleteSysDict(id);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 根据查询条件导出字典
     *
     * @param response 响应对象
     * @param paramMap 参数Map
     */
    @ApiOperation(value = "根据查询条件导出字典", tags = {"数据字典"})
    @PostMapping(path = "/exportSysDict")
    public void exportSysDict(HttpServletResponse response, @RequestParam Map<String, Object> paramMap) {
        try {
            List<String> headList = Arrays.asList("ID", "字典名称", "字典值", "字典类型", "排序", "上级字典ID", "创建时间");
            List<LinkedHashMap<String, Object>> dataList = sysDictService.querySysDictForExcel(paramMap);
            ExcelUtils.exportExcel(headList, dataList, "字典管理", response);
        } catch (Exception e) {
            logger.warn(e.toString());
        }
    }

    /**
     * 根据字典类型查询下拉框数据列表
     *
     * @param dictType 字典类型
     * @return
     */
    @ApiOperation(value = "根据字典类型查询下拉框数据列表", tags = {"数据字典"})
    @GetMapping(value = "/queryDictByDictType")
    public List<LinkedHashMap<String, Object>> queryDictByDictType(@RequestParam(name = "dictType") String dictType) {
        return sysDictService.queryDictByDictType(dictType);
    }

    /**
     * 查询字典类型数据列表
     *
     * @param dictType 字典类型
     * @return
     */
    @ApiOperation(value = "查询字典类型数据列表", tags = {"数据字典"})
    @GetMapping(value = "/getSysDictByType")
    public ListResult<Object> getSysDictByType(@RequestParam(name = "dictType") String dictType) {
        List<LinkedHashMap<String, Object>> list = sysDictService.getSysDictByType(dictType);
        return ResultBuilder.buildListSuccess(list);
    }

    /**
     * 查询字典类型数据列表，用于前端渲染数据
     *
     * @return
     */
    @ApiOperation(value = "查询字典类型数据列表，用于前端渲染数据", tags = {"数据字典"})
    @GetMapping(value = "/getSysDictList")
    public ListResult<Object> getSysDictList() {
        List<LinkedHashMap<String, Object>> list = sysDictService.getSysDictList();
        return ResultBuilder.buildListSuccess(list);
    }

}
